package com.example.text.cyclone



import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.text.R
import com.example.text.app.bold
import com.example.text.app.normal
import com.example.text.getAttrValue

//import com.jczy.cyclone.R
//import com.jczy.cyclone.app.bold
//import com.jczy.cyclone.app.getAttrValue
//import com.jczy.cyclone.app.normal

/**
 * Created by ZengCS on 2023/3/22.
 * E-mail:zengcs@vip.qq.com
 * Add:中国成都
 * Desc: 自定义Tab指示器助手
 */
class CustomIndicatorHelper(
    private val context: Context,
    private val tabList: List<TextView>,
    private val indicator: View,
    private val offset: Float,
    private val duration: Long = 100L
) {
    private val colorTheme by lazy {
        ContextCompat.getColor(context, getAttrValue(context, R.attr.colorTheme))
    }
    private val colorDarkText by lazy {
        ContextCompat.getColor(context, getAttrValue(context, R.attr.colorDarkText))
    }
    private var animating = false

    fun moveIndicator(pos: Int) {
        if (animating)
            return
        animating = true
        val x0 = indicator.translationX
        val x1 = pos * offset
        if (x0 == x1) {
            updateTextStyle(pos)
            return
        }
        val anim = ObjectAnimator.ofFloat(indicator, "translationX", x0, x1)
        anim.duration = duration
        anim.start()
        anim.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                // 动画结束了
                updateTextStyle(pos)
            }
        })
    }

    private fun updateTextStyle(pos: Int) {
        animating = false
        tabList.forEachIndexed { index, textView ->
            textView.setTextColor(if (pos == index) colorTheme else colorDarkText)
            if (pos == index) {
                textView.bold(18f)
            } else {
                textView.normal(16f)
            }
        }
    }
}